package com.qfmy.webapp.controller.novel;

import com.qfmy.common.result.Result;
import com.qfmy.model.vo.novel.HistoryVo;
import com.qfmy.webapp.service.novel.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.controller.novel
 * @date 2025/11/13
 * @description 阅读历史控制类
 */
@RestController
@RequestMapping("/app/history")
public class HistoryController {

    /**
     * 注入阅读历史服务类
     */
    @Autowired
    private HistoryService historyService;


    /**
     * 分页获取阅读历史
     */
    @RequestMapping("/getHistories")
    public Result<List<HistoryVo>> getHistories(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        List<HistoryVo> histories = historyService.getHistories(pageNum, pageSize);
        return Result.ok(histories);
    }

    /**
     * 根据历史id删除阅读历史
     */
    @RequestMapping("/deleteHistory")
    public Result deleteHistory(@RequestParam Integer[] historyIds) {
        historyService.deleteHistory(historyIds);
        return Result.ok();
    }

    /**
     * 清除所有阅读历史
     */
    @RequestMapping("/clearAllHistories")
    public Result clearAllHistories() {
        historyService.clearAllHistories();
        return Result.ok();
    }

    /**
     * 保存阅读历史
     * @param novelId 小说id
     * @param chapterIndex 章节序号
     * @param page 阅读进度
     * @param title 章节名称
     * @return
     */
    @RequestMapping("/saveHistory")
    public Result saveHistory(@RequestParam Integer novelId, @RequestParam Integer chapterIndex, @RequestParam double page,@RequestParam String title) {
        historyService.saveHistory(novelId, chapterIndex, page, title);
        return Result.ok();
    }

    /**
     * 根据小说id获取浏览历史
     */
    @RequestMapping("/getHistoriesByNovelId")
    public Result<HistoryVo> getHistoriesByNovelId(@RequestParam Integer novelId) {
        HistoryVo historyVo = historyService.getHistoriesByNovelId(novelId);
        return Result.ok(historyVo);
    }


}
